<template>
	<view class="login">
		<view class="bindPhone" v-if="user != null">
			<button type="default" hover-class="none" open-type="getPhoneNumber"
				@getphonenumber="confirm_phone_number_event">手机绑定</button>
		</view>
	</view>
</template>

<script>
	const app = getApp();
	export default {
		data() {
			return {
				phone: "",
				sjcode: "",
				openId:"",
				token:"",
				phone:"",
				user: null,
				scene:"",
				params: null,
				spreadUid:'',
				numberId:'',
				clearCache:"",
				entryTime:0,
			}
		},
		// 分享给朋友
		onShareAppMessage(res) {
			if(res.from === 'button'){
				console.log(res.target,"target");
			}
			return{
				title:app.globalData.data.tenantName,
				path:"pages/login/login?numberId=" + app.globalData.data.numberId + "&clearCache=true",
				mpId:app.globalData.data.appid
			}
		},
		// 分享朋友圈
		// onShareTimeline(res) {
		// 	return{
		// 		title:app.globalData.data.tenantName,
		// 		type:0,
		// 		summary:""
		// 	}
		// },
		onLoad(params) {
			this.params = params;
			if (!(params.scene == undefined)) { //判断获取到参数没有
				this.scene = decodeURIComponent(params.scene); // 转义参数
			}
			this.clearCache = this.params.clearCache || '';
			if(!typeof(clearCache)=='undefined') this.clearCache =clearCache;
		},
		onShow() {
			if(this.scene!='' || this.clearCache!=''){
				// 清空缓存
				uni.clearStorageSync();
				var user = app.globalData.get_user_info(this, "init",this.scene);
			}else{
				var user = app.globalData.get_user_cache_info();
				this.user = user;
				app.globalData.data.numberId = this.user.numberId;
				// app.globalData.data.spreadUid = this.user.spreadUid;
			}
			// 记录用户进来时间
			this.entryTime = Date.now();
		},
		onUnload() {
			this.OnEntryTime();
		},
		methods: {
			OnEntryTime(){
				var radarlog_data = {
					pageUrl:"pages/login/login",
					name:"登录页",
					remark:"登录页停留时间",
					entryTime:this.entryTime,
				}
				uni.request({
					url:app.globalData.get_request_url("radarlog/add"),
					data:radarlog_data,
					method:"POST",
					success: (res) => {
						console.log(res,"登录页停留时间");
					}
				})
			},
			init(){
				uni.switchTab({
					url:"/pages/index/index"
				})
			},
			// 手机绑定
			confirm_phone_number_event(e) {
				var self = this;
				let user = app.globalData.get_login_cache_info();
				let tenantId = app.globalData.data.tenantId;
				let url = app.globalData.data.request_url + 'onekeyusermobilebind?tenantId=' + tenantId +
					'&token=' + user.token;
				var encrypted_data = e.detail.encryptedData || null;
				var iv = e.detail.iv || null;
				var code = e.detail.code || null;
				uni.showLoading({
					title: '处理中...'
				});
				var event_callback = self.params.event_callback || null;
				uni.request({
					url: url,
					method: "POST",
					data: {
						"encrypted_data": encrypted_data,
						"iv": iv,
						"code": code,
						"openid": user.openId
					},
					success: (res) => {
						uni.hideLoading();
						uni.showToast({
							title: '手机绑定成功'
						})
						// uni.switchTab({
						// 	url:"/pages/index/index"
						// })
						uni.setStorageSync(app.globalData.data.cache_user_info_key, res.data.data);
						setTimeout(function() {
						    var pages = getCurrentPages();
						    if(pages.length > 1) {
						        // 触发回调函数
						        if (event_callback != null) {
						            if((pages[pages.length-2][event_callback] || null) != null) {
						                pages[pages.length-2][event_callback]();
						            }
						        }
						        // 默认返回上一页
						        uni.navigateBack();
						    }
						}, 1000);
					},
					fail: (err) => {
						uni.showToast({
							title: "服务器请求出错"
						})
					}
				})
			}
			
		}
	}
</script>

<style lang="less">
	.login {
		width: 100%;
		height: 100vh;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;

		.wxlogin {
			width: 95%;

			button {
				background-color: greenyellow;
				color: white;
			}
		}

		.bindPhone {
			width: 95%;
			margin-top: 40rpx;

			button {
				background-color: red;
				color: white;
			}
		}
	}

	.login_bj {
		position: relative;
		height: 100vh;
		display: flex;
		justify-content: center;
		align-items: center;

		image {
			height: 100%;
			width: 100%;
			// margin-top: 10px;
		}

		.login {
			width: 100%;
			position: absolute;
			top: 40%;
			left: 16%;
			transform: translateX(-40%, -16%);

			.login_inp {
				display: flex;
				align-items: center;
				margin-top: 10px;

				input {
					width: 150px;
					height: 30px;
					text-align: left;
					line-height: 30px;
					border-radius: 20px;
					background-color: #f3f1f4;
					padding-left: 10px;
					font-size: 10px;
				}

				.code {
					width: 90px;
					font-size: 10px;
				}

				button {
					width: 80px;
					height: 30px;
					line-height: 30px;
					text-align: center;
					font-size: 10px;
					margin-left: 8px;
				}
			}
		}

		.wxLogin {
			position: absolute;
			top: 57%;
			left: 34%;
			transform: translateX(-57%, -34%);

			button {
				background-color: green;
				color: white;
				font-size: 13px;
			}
		}

		.wxbinding {
			position: absolute;
			top: 70%;
			left: 34%;
			transform: translateX(-70%, -34%);

			button {
				background-color: green;
				color: white;
			}
		}
	}
</style>